//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.rjxy.demo.controller;

import com.rjxy.demo.pojo.Users;
import com.rjxy.demo.server.UsersServer;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"/user"})
@SessionAttributes({"userInfo"})
public class UsersController {
    @Autowired
    private UsersServer usersServer;
    private static final Logger log = Logger.getLogger(UsersController.class);

    public UsersController() {
    }

    @RequestMapping({"findAll"})
    public String findAll(Model model) {
        System.out.println("开始查询全部用户信息");
        List<Users> list = this.usersServer.findAll();
        model.addAttribute("userList", list);
        return "home";
    }

    @RequestMapping(
            value = {"/login"},
            method = {RequestMethod.POST}
    )
    public String login(Users users, Model model, RedirectAttributes ra) {
        log.info("进入登陆方法，参数：" + users);
        users = this.usersServer.login(users);
        if (users != null) {
            log.info("登陆成功，返回结果：" + users);
            model.addAttribute("userInfo", users);
            return "redirect:/home";
        } else {
            log.info("登陆失败");
            ra.addFlashAttribute("message", "用户名密码有误");
            return "redirect:/login";
        }
    }

    @PostMapping({"/register"})
    public String register(Users users) {
        log.info("执行注册请求，参数：" + users);
        if (users.getUname() != null && users.getUname().equals("")) {
        }

        this.usersServer.register(users);
        log.info("注册成功，跳转登陆页面");
        return "redirect:/login";
    }
}
